<?php




/**
* Add an entity in [ colors ]
*/

$colors = new colors();

if($variable->post('add-action', 'string', ''))
{
	# Posted Data: Apply security
	$data = $variable->post('colors', 'array', array());
	
	# Immediately activate the record
	$data['is_active']   = 'Y';
	$data['is_approved'] = 'Y';
	
	# When this record is added for the first time?
	$data['added_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	
	$data['subdomain_id'] = $subdomain_id;
	$data['subdomain_id'] = strtoupper($data['color_foreground']);
	$data['subdomain_id'] = strtoupper($data['color_background']);
	
	if($color_id = $colors->add($data, false))
	{
		# Send a welcome message (and ask to authenticate), if applies, eg. as in Membership
		# $colors->welcome_first($color_id);

		$messenger = new messenger('success', 'The record has been added.');

		stopper::url(url::last_page('colors-list.php'));
		# stopper::url('colors-add-successful.php');
		# stopper::url('colors-list.php');
		
		# Once successfully added, as the admin user to edit the data.
		# Inspired by some financial applications that allow to edit the record as soon as it is added.
		# @todo Generate it with the help of color_id and protection code
		# $code = '';
		# stopper::url("colors-edit.php?id={$color_id}&code={$code}");
	}
	else
	{
		$messenger = new messenger('error', 'The record was NOT added.');

		$_SESSION['attempt_colors'] = $data;
		stopper::url('colors-add.php');
		
		#stopper::url('colors-add-error.php');
	}
}
else
{
	# Must allow a chance to load the ADD form.
	# stopper::url('colors-direct-access-error.php');

	# Purpose of this code block is to make sure that the variable
	# gets all indices with blank data, to feed to ADD form.
	
	# A dynamic details about this record
	$details = array();
	
	if(isset($_SESSION['attempt_colors']))
	{
		$details = $_SESSION['attempt_colors'];
		unset($_SESSION['attempt_colors']);
	}

	# Validate it against the parameters in itself, plus those what we need.
	$details = $colors->validate('add', $details);
	$smarty->assign('colors', $details);
}

$smarty->assign('protection_code', $colors->code());
?>